package com.music.controller;

import com.github.pagehelper.PageInfo;
import com.music.dto.Result;
import com.music.entity.Admin;
import com.music.entity.District;
import com.music.entity.Singer;
import com.music.service.SingerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Singer")
@Slf4j
public class SingerController {
    @Autowired
    private SingerService singerService;
    @RequestMapping("/list")
    public Result doFindSingerList(String singerAbbr,Integer districtId,String singerSex,Integer singerCategoryId){
        Result result=new Result();
        List<Singer> list=new ArrayList<Singer>();
        Singer singer=new Singer();
        if(singerAbbr!=null&&!"".equals(singerAbbr)){
            singer.setSingerAbbr(singerAbbr);
        }
        if(districtId!=null&&!"".equals(districtId)){
            singer.setDistrictId(districtId);
        }
        if(singerSex!=null&&!"".equals(singerSex)){
            singer.setSingerSex(singerSex);
        }
        if(singerCategoryId!=null&&!"".equals(singerCategoryId)){
            singer.setSingerCategoryId(singerCategoryId);
        }
        list=singerService.findByCondition(singer);
        if(!list.isEmpty()){
            result.ok(list);
        }else{
            result.fail(0,"查无数据");
        }
        return result;
    }



    @GetMapping("/singerPageList")
    public @ResponseBody Result getPageSinger(String singerName,int pageNum, int pageSize){
        Result result = new Result();
        Singer singer = new Singer();
        singer.setSingerName(singerName);
        PageInfo<Singer> singerByPage = singerService.getSingerByPage(pageNum, pageSize, singer);
        if (singerByPage != null){
            result.ok(singerByPage);
        }else {
            result.fail(0,"无数据");
        }
        return result;
    }

    @PostMapping("/addSinger")
    public @ResponseBody Result addOperateSinger(@RequestParam String singerId,@RequestParam String singerName,@RequestParam String singerAbbr,@RequestParam Integer singerCategoryId,@RequestParam String singerSex,@RequestParam Integer singerStatus, @RequestParam String singerImage,@RequestParam String singerDescription,@RequestParam String singerNationality,@RequestParam String singerBirthday,@RequestParam Integer districtId){
        Result result = new Result();
        Singer singer = new Singer();
        singer.setSingerId(singerId);
        singer.setSingerName(singerName);
        singer.setSingerAbbr(singerAbbr);
        singer.setSingerCategoryId(singerCategoryId);
        singer.setSingerSex(singerSex);
        singer.setSingerStatus(singerStatus);
        singer.setSingerImage(singerImage);
        singer.setSingerDescription(singerDescription);
        singer.setSingerNationality(singerNationality);
        singer.setSingerBirthday(singerBirthday);
        singer.setDistrictId(districtId);
        //调用业务层
        boolean b = singerService.addSinger(singer);
        if (b){
            result.ok(true).setMessage("添加成功");
        }else {
            result.fail(0,"添加失败,可能原因是它已存在");
        }
        return result;
    }

    @PostMapping("/updateSinger")
    public @ResponseBody Result updateOperateSinger(@RequestParam String singerId,@RequestParam String singerName,@RequestParam String singerAbbr,@RequestParam Integer singerCategoryId,@RequestParam String singerSex,@RequestParam Integer singerStatus, @RequestParam String singerImage,@RequestParam String singerDescription,@RequestParam String singerNationality,@RequestParam String singerBirthday,@RequestParam Integer districtId){
        Result result = new Result();
        Singer singer = new Singer();
        singer.setSingerId(singerId);
        singer.setSingerName(singerName);
        singer.setSingerAbbr(singerAbbr);
        singer.setSingerCategoryId(singerCategoryId);
        singer.setSingerSex(singerSex);
        singer.setSingerStatus(singerStatus);
        singer.setSingerImage(singerImage);
        singer.setSingerDescription(singerDescription);
        singer.setSingerNationality(singerNationality);
        singer.setSingerBirthday(singerBirthday);
        singer.setDistrictId(districtId);
        //调用业务层
        boolean m = singerService.modifySinger(singer);
        if (m){
            result.ok(true).setMessage("修改成功");
        }else {
            result.fail(0,"修改失败,可能原因是它不存在");
        }
        return result;
    }


}
